package com.gduf.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gduf.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gduf.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gduf.gulimall.product.entity.AttrEntity;
import com.gduf.gulimall.product.entity.vo.AttrGroupEntityVo;
import com.gduf.gulimall.product.service.AttrAttrgroupRelationService;
import com.gduf.gulimall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gduf.gulimall.product.entity.AttrGroupEntity;
import com.gduf.gulimall.product.service.AttrGroupService;
import com.gduf.common.utils.PageUtils;
import com.gduf.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author masterXiao
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:42:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;


    @Resource
    private AttrAttrgroupRelationService relationService;

    // /225/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getGroupWithAttr(@PathVariable(value = "catelogId",required = true) long catelogId){
        List<AttrGroupEntityVo> entityVos = attrGroupService.queryGroupWithAttr(catelogId);
        return R.ok().put("data",entityVos);
    }

    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> list){
        relationService.saveBatch(list);
        return R.ok();
    }

    // /product/attrgroup/1/noattr/relation?t=1683177861656&page=1&limit=10&key=
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable("attrGroupId") long attrGroupId,
                               @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.getNoAttrRelation(params,attrGroupId);
        return R.ok().put("page",page);
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable long attrgroupId){
        List<AttrEntity> data =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @PostMapping("/attr/relation/delete")
    public R deleteRelations(@RequestBody AttrAttrgroupRelationEntity[] entities){

        relationService.deleteRelations(entities);
        return R.ok();
    }

}
